package com.itheima.web.controller.oms;

import com.itheima.result.AjaxResult;
import com.itheima.result.PageResult;
import com.itheima.service.oms.OmsOrderReturnReasonService;
import com.itheima.vo.oms.OmsOrderReturnReason;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * 退货原因表 前端控制器
 *
 */
@RestController
@RequestMapping("/returnReason")
public class OmsOrderReturnReasonController {

    @DubboReference
    private OmsOrderReturnReasonService orderReturnReasonService;

    /**
     * 分页查询退货原因
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Integer pageNum,Integer pageSize) {
        PageResult result = orderReturnReasonService.listPage(pageNum, pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 根据id，获取单个退货原因详情信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult select(@PathVariable Long id) {
        OmsOrderReturnReason orderReturnReason = orderReturnReasonService.getById(id);
        return AjaxResult.success(orderReturnReason);
    }

    /**
     * 修改退货原因
     *
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateReturnReason(@PathVariable Long id, @RequestBody OmsOrderReturnReason reason) {
        Boolean count = orderReturnReasonService.updateReturnReason(id, reason);
        if (count) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 修改退货原因启用状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status")
    public AjaxResult updateStatus(@RequestParam("ids") Long id, @RequestParam Integer status) {
        OmsOrderReturnReason reason = new OmsOrderReturnReason();
        reason.setId(id);
        reason.setStatus(status);
        orderReturnReasonService.updateById(reason);
        return AjaxResult.success("修改成功");
    }

    /**
     * 批量删除退货原因
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam List<Long> ids) {
        Boolean count = orderReturnReasonService.removeByIds(ids);
        if (count) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 添加退货原因
     * @param reason
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody OmsOrderReturnReason reason) {
        reason.setCreateTime(LocalDateTime.now());
        Boolean count = orderReturnReasonService.save(reason);
        if (count ) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");

    }
}